package com.eastfair.pay.service.impl;

import com.eastfair.core.context.ContextUtil;
import com.eastfair.pay.dao.InvoiceApplyerRelationMapper;
import com.eastfair.pay.entity.InvoiceApplyerRelation;
import com.eastfair.pay.service.InvoiceApplyerRelationService;
import com.eastfair.boot.service.SuperServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import java.util.Collection;
//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;

import com.eastfair.pay.dto.*;
import com.eastfair.pay.vo.InvoiceApplyerRelationVO;

/**
* <p>
    * 业务实现类
    * 发票申请
    * </p>
*
* @author clm
* @date 2022-08-06
*/
@Slf4j
@Service
//@DS("#thread.tenant")
public class InvoiceApplyerRelationServiceImpl extends SuperServiceImpl<InvoiceApplyerRelationMapper, InvoiceApplyerRelation> implements InvoiceApplyerRelationService {

    /**
     * 基础数据
     * @param model
     * @return
     */
    @Override
    protected R<Boolean> handlerSave(InvoiceApplyerRelation model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        //雪花ID
        return R.successDef();
    }

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<InvoiceApplyerRelation> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<InvoiceApplyerRelation> list(InvoiceApplyerRelationDTO modelDTO, List<Long> ids) {
        return ((InvoiceApplyerRelationService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'isEnabled', #args[0]?.isEnabled},"+
       "{'applyId', #args[0]?.applyId},"+
       "{'businessId', #args[0]?.businessId},"+
        "{'id', #args[1]}"+
        "}"))
    public List<InvoiceApplyerRelation> listOfCache(InvoiceApplyerRelationDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<InvoiceApplyerRelation> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(modelDTO.getApplyId() != null,InvoiceApplyerRelation::getApplyId,modelDTO.getApplyId());
                    queryWrapper.lambda().eq(modelDTO.getBusinessId() != null,InvoiceApplyerRelation::getBusinessId,modelDTO.getBusinessId());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(InvoiceApplyerRelation::getId, ids);
            }
            queryWrapper.lambda().eq(InvoiceApplyerRelation::getIsDeleted, BusinessConstant.DELETE_NO);
            List<InvoiceApplyerRelation> modelList = list(queryWrapper);
            return modelList;
    }

    @Override
    public InvoiceApplyerRelation getByBuinessId(Long buinessId) {
        QueryWrapper<InvoiceApplyerRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", buinessId);
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public InvoiceApplyerRelationVO doToVo(InvoiceApplyerRelation model) {
        InvoiceApplyerRelationVO modelVo = new InvoiceApplyerRelationVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<InvoiceApplyerRelationVO> doToVos(List<InvoiceApplyerRelation> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<InvoiceApplyerRelationVO> result = new ArrayList<>();
        for (InvoiceApplyerRelation model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
